package com.ke.controller;

import com.ke.pojo.*;
import com.ke.service.VideoService;
import com.ke.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author bochen
 */
@RestController
@RequestMapping("/video")
@CrossOrigin(allowCredentials = "true")
public class VideoController {
    public  RePackage rePackage=new RePackage();
    @Autowired
    VideoService videoService;
    @Autowired
    RedisTemplate redisTemplate;
    @RequestMapping("/addfile")
        public String addfile(MultipartFile file){
      /* String id = (String)request.getAttribute("id");
        if(id==null){
            rePackage.setCode(505);
            rePackage.setMessage("请先登录");
            return "请先登录";
        }*/
        String uodownoic = UploadUtil.upload(file);
        return uodownoic;

    }
    @RequestMapping("/addvideopic")
    public String addvideopic(MultipartFile file){
       /* String id = (String)request.getAttribute("id");
        if(id==null){
            rePackage.setCode(505);
            rePackage.setMessage("请先登录");
            return "请先登录";
        }*/
        String uodownoic = UploadUtil.upload(file);
        return uodownoic;

    }

    @RequestMapping("/addvideo")
    public RePackage add(@RequestBody VideoFile videoFile, HttpServletRequest request){
      String id = (String) request.getAttribute("id");
      System.out.println(id);
        //跟id存储方法有关，当id失效，返回登录页面
        if (id==null){
            rePackage.setCode(505);
            rePackage.setMessage("未登录");
            return rePackage;
        }

        Video v=new Video();
        v.setCid(videoFile.getVid());
        v.setUid(Integer.parseInt(id));
        v.setVname(videoFile.getVname());
        v.setVaddress(videoFile.getVaddress());
        v.setPrice(Integer.parseInt(videoFile.getPrice()));
        v.setPic(videoFile.getPic());
        RePackage add = videoService.add(v);

        if (add!=null){
            //每次添加新的video，删除原有redis
            long listvideo = redisTemplate.opsForList().size("listvideo");
            redisTemplate.opsForList().remove("listvideo",0,listvideo);
        }

        return add;

    }
    @RequestMapping("/findid")
    public RePackage findid(@RequestBody Map map){
        Integer id=(Integer) map.get("id");
        RePackage findid = videoService.findid(id);
        return findid;
    }
    @RequestMapping("/findall")
    public RePackage findall(Integer page,Integer size){
        RePackage getall = videoService.getall(page, size);
        return getall;
    }
    //首页价格排序
    @RequestMapping("/priceall")
    public RePackage getpriceall(@RequestBody Map map){
    return videoService.findpriceall((Integer) map.get("size"));
    }
    //增加点赞数，页面触发返过来一个1，数据库对应字段加1
    @RequestMapping("/addlike")
    //id为视频id
    public RePackage likenumadd(@RequestBody Map map){
    return videoService.likeadd((Integer) map.get("id"));
    }
    //查询点赞数
    @RequestMapping("/findlike")
    //视频id
    public RePackage findlike(@RequestBody Map map){
        return videoService.findlike((Integer) map.get("id"));
    }
    //增加收藏数
 @RequestMapping("/addcollect")
    public RePackage addcollect(@RequestBody Map map,HttpServletRequest request){
     String uid1 = (String)request.getAttribute("id");

     if(uid1==null){
         rePackage.setCode(505);
         rePackage.setMessage("请先登录");
         return rePackage;
     }
     Integer uid = Integer.parseInt(uid1);
     Integer vid = (Integer) map.get("id");
     return videoService.addcollect(vid,uid);
 }
 //查询收藏数
    @RequestMapping("/findcollect")
    public RePackage findcollect(@RequestBody Map map){
        return videoService.findcollect((Integer) map.get("id"));
    }
//添加评论
    @RequestMapping("/addcomment")
    public RePackage addcomment(@RequestBody VideoCommenttext videoCommenttext, HttpServletRequest request){
        String uid1 = (String)request.getAttribute("id");
        if (uid1==null){
            rePackage.setCode(505);
            rePackage.setMessage("请先登录");
            return rePackage;
        }
        Integer uid= Integer.parseInt(uid1);
        Integer vid =videoCommenttext.getVid();
        //获取前端评论文字
        String comment_words=videoCommenttext.getTextarea();
        RePackage addcomment = videoService.addcomment(vid, uid, comment_words);
        return addcomment;
    }

    //获取评论
    @RequestMapping("/findcomment")
    public RePackage findcomment(@RequestBody Map map){
        return videoService.findcomment((Integer)map.get("id"));
    }

    }


